'''
Created on Oct 14, 2009

@author: mkiyer
'''

from veggie.app.application import CommandLineApplication
from veggie.app.rnaseq import sw_root
import veggie.db.refdb as refdb
import logging
import os

class Cufflinks(CommandLineApplication):
    '''
    Cufflinks is a program that assembles aligned RNA-Seq reads into 
    transcripts and then estimates their abundances.

    http://cufflinks.cbcb.umd.edu/manual.html#cufflinks
    '''
    _executable = os.path.join(sw_root, 'cufflinks', 'current', 'cufflinks')
    
    def _get_results_paths(self):
        basepath = os.path.abspath(self.cwd)
        return {'gtf': os.path.join(basepath, 'transcripts.gtf'),
                'transcripts': os.path.join(basepath, 'transcripts.expr'),
                'genes': os.path.join(basepath, 'genes.expr')}
    
    def run(self, sam_file, 
            inner_dist_mean, 
            num_threads=2,
            label="CUFF",
            options=None, 
            cwd=None):
        if options:
            self.addOptions(options)
        if cwd:
            self.cwd = cwd

        self.addOptions({'--inner-dist-mean': inner_dist_mean,
                         '-s': 20, 
                         '-p': num_threads,
                         '--label': label})
        self.args = [sam_file]
        logging.debug(self.getCommandLine())
        return self()

class CuffCompare(CommandLineApplication):
    '''
    Cufflinks includes a program that you can use to help analyze the 
    transfrags you assemble. The program cuffcompare helps you:
    * Compare your assembled transcripts to a reference annotation
    * Track Cufflinks transcripts across multiple experiments 
      (e.g. across a time course)
    '''
    _executable = os.path.join(sw_root, 'cufflinks', 'current', 'cuffcompare')

    def run(self, transcripts_gtf, 
            ref_gtf_file=None,
            outfile=None):
        if ref_gtf_file is not None:
            self.addOption('-r', ref_gtf_file)
        if outfile is not None:
            self.addOption('-o', outfile)
        self.addOptions({'-s': refdb.get_hg18_illumina_sequences_path(),
                         '-R': None,
                         '-o': outfile})
        self.args = [transcripts_gtf]
        logging.debug(self.getCommandLine())
        return self()
        


